package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/permission")
public class PermissionController {
    
    @Reference
    private PermissionService permissionService;

    //新增权限数据
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"权限添加失败");
        }
        return  new Result(true,"权限添加成功");
    }

    //分页查询权限数据
    @RequestMapping("/findPage")
    public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = permissionService.pageQuery(queryPageBean);
        return result;
    }

    //删除权限数据
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        return permissionService.deleteById(id);
    }

    //编辑权限数据
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission) {

        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"权限修改失败");
        }
        return new Result(true,"权限修改成功");
    }

    //通过ID查找权限数据
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Permission permission = null;
        try {
            permission = permissionService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false,"权限回显失败");
        }
        return new Result(true,"权限回显成功",permission);
    }

}

